package com.example.myapplication;



import static com.example.myapplication.R.*;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class FileMainMainActivity extends AppCompatActivity {

    TextView textView1;
    Button button1,button2,button3,button4;
    SharedPreferences sp;
    FileOutputStream outputStream;
    FileInputStream inputStream;
    FileReader fileReader;
    String str1,str2;
    Myopenhelper myopenhelper;
    SQLiteDatabase database;

    //protected方法可以调用package中的所有方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_file_main_main);

        button1=findViewById(R.id.button_file_1);
        button2=findViewById(R.id.button_file_2);
        button3=findViewById(R.id.button_file_3);
        button4=findViewById(R.id.button_file_4);
        textView1=findViewById(R.id.textView_file_1);

        sp=getSharedPreferences("kingfile",Context.MODE_PRIVATE);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sp.edit().putString("name","king").putString("key","123456").commit();
                Log.d("king","Data saved"+sp.getString("name","default"));

            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView1.setText(sp.getString("name","king"));
            }
        });
        File file=new File("/data/data/com.example.myapplication/king.txt");

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                try {
                    outputStream =new FileOutputStream(file);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                try {
                    outputStream.write("hello".getBytes());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    FileInputStream inputStream = new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                };
                try {
                    inputStream=new FileInputStream(file);
                    InputStreamReader reader=new InputStreamReader(inputStream);
                    str1=reader.getEncoding();
                    //fileReader=new FileReader(file);
                    BufferedReader bufferedReader=new BufferedReader(reader);
                    str2=bufferedReader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                };
                textView1.setText(str1+"/"+str2);

            }
        });

        myopenhelper=new Myopenhelper(this,"kingDB",null,1);
        database=myopenhelper.getWritableDatabase();
        database.execSQL("CREATE TABLE person (ID INT PRIMARY KEY AUTOINCREMENT, age INT,name VARCHAR(100) NOT NULL)");

    }
}